/*
 * Picture2D.cpp
 *
 *  Created on: 18.2.2013
 *      Author: Travis
 */

#ifdef _WIN32 
#include <windows.h> 
#endif

#include "Picture2D.h"

namespace Gadgets
{

Picture2D::Picture2D(int _width, int _height, std::string _file)
{
	width = _width;
	height = _height;
	file = _file;

	// load texture
	if (Textures::GUI_textures != NULL)
	{
		Textures::GUI_textures->loadImage(file.c_str());
		texture = Textures::GUI_textures->getTexIdByName(file);
	}
	else
	{
		std::cerr << "Unable to load Gui Texture Pool" << std::endl;

	}


	shaderID = Shader::sPool->getProgramID("form");

	visible = true;

}

Picture2D::~Picture2D()
{
}

void Picture2D::draw(glm::vec2 Pos, int addsize)
{
	glm::mat4 ViewMatrix = glm::translate(glm::mat4(1.0),
			glm::vec3(Pos.x - (addsize / 2), Pos.y - (addsize / 2), 0.0));
	glm::mat4 P = World::world->get2DProjection() * ViewMatrix;

	glUseProgram(shaderID);

	glUniformMatrix4fv(glGetUniformLocation(shaderID, "P"), 1, GL_FALSE,
			&P[0][0]);
	glUniform1i(glGetUniformLocation(shaderID, "tex"), 0);

	glDisable(GL_DEPTH_TEST);
	glDisable(GL_LIGHTING);
	glEnable(GL_BLEND);

	glEnable(GL_TEXTURE_2D);
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, texture);

	glBegin(GL_QUADS);
	glTexCoord2i(0, 0);
	glVertex2i(0, 0);
	glTexCoord2i(1, 0);
	glVertex2i(width + addsize, 0);
	glTexCoord2i(1, 1);
	glVertex2i(width + addsize, height + addsize);
	glTexCoord2i(0, 1);
	glVertex2i(0, height + addsize);
	glEnd();

	glDisable(GL_TEXTURE_2D);
	glDisable(GL_BLEND);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_LIGHTING);

	glUseProgram(0);
}

void Picture2D::draw(glm::vec2 Pos)
{
	draw(Pos, 0);
}

const std::string& Picture2D::getFile() const
{
	return file;
}

void Picture2D::setFile(const std::string& file)
{
	this->file = file;
}

int Picture2D::getHeight() const
{
	return height;
}

void Picture2D::setHeight(int height)
{
	this->height = height;
}

const std::string& Picture2D::getPath() const
{
	return path;
}

void Picture2D::setPath(const std::string& path)
{
	this->path = path;
}

int Picture2D::getWidth() const
{
	return width;
}

void Picture2D::setWidth(int width)
{
	this->width = width;
}

bool Picture2D::isVisible() const
{
	return visible;
}

void Picture2D::setVisible(bool visible)
{
	this->visible = visible;
}

} /* namespace Gadgets */
